From 55280548991b59858092411037f88d13093dfc67 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Thu, 15 Aug 2024 15:26:32 -0500 Subject: [PATCH] Report on non-printing characters in bad column headings --- src/pgwui_upload_core/views/upload.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/pgwui_upload_core/views/upload.py b/src/pgwui_upload_core/views/upload.py index 39aa3d6..17a4522 100644 --- a/src/pgwui_upload_core/views/upload.py +++ b/src/pgwui_upload_core/views/upload.py @@ -277,6 +277,21 @@ class InsertStmt: stmt = attrs.field(default=None) cols = attrs.field(default=None) + def report_nonprintable(self, chars): + if chars.isprintable(): + return '' + + positions = [] + values = [] + for pos, char in enumerate(chars, start=1): + if not char.isprintable(): + positions.append(str(pos)) + values.append(char.encode(errors='ignore').hex()) + + return (' [Note: The character(s) in position(s)' + f' {", ".join(positions)}' + f' are non-printable, in hex: {", ".join(values)}]') + def report_bad_cols(self, qualified_table, bad_cols, quotecols): if quotecols: detail = ('

The following columns are not in the ({0})' @@ -297,14 +312,15 @@ class InsertStmt: ' or the table has column names containing' ' upper case characters, or you do not have' ' permission to access the columns:

' + detail.append(f'
  • {markupsafe.escape(bad_col)}' + f'{self.report_nonprintable(bad_col)}
  • ') + detail.append('') raise upload_ex.BadHeadersError( 'Header line contains unknown column names', - detail=detail) + detail=''.join(detail)) def build_insert_stmt( self, tuh, data, qualified_table, quotecols, column_quoter): -- 2.34.1